<?php
/**
 * User: ppjun378
 * Data: 2019/01/23
 * Time: 18:30
 */
namespace App\Service;

use App\Model\FamilySituations;

class FamilySituationsService
{
    /**
     *  获取数据
     * @param $request
     * @return mixed
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function dataList($resume_id)
    {
        $fields = [
            'id as fid', // 家庭关系id
            'full_name', //姓名
            'relationship', //关系
            'engage_in_work', //从事工作
            'link_phone', //联系电话
            'resume_id', //简历编号
        ];

        $data = FamilySituations::select($fields)
            ->where('resume_id', $resume_id)
            ->orderBy('id', 'asc')
            ->get();

        $data = empty($data) ? array() : $data->toArray();

        return $data;

    }

    /**
     * 获取详情
     * @param int $id
     * @return mixed
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function getDetail($request)
    {

    }

    /**
     * 添加/更新
     * @param $request
     * @return mixed
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function store($request, $id)
    {

        $family_situations = $request->input('family_situations', '');
        // $family_situations = '[{"fid": "1","full_name": "姓名1dfasf","relationship": "父亲","engage_in_work": "务农","link_phone": "13038888888","resume_id": 2},{"fid": "","full_name": "姓名1","relationship": "父亲","engage_in_work": "务农","link_phone": "13038888888","resume_id": 2}]';

        if (empty($family_situations)) {
            return false;
        }

        $family_situations = json_decode($family_situations, true);

        $map = [];
        foreach ($family_situations as $key => $val) {
            $map['full_name'] = $val['full_name'];
            $map['relationship'] = $val['relationship'];
            $map['engage_in_work'] = $val['engage_in_work'];
            $map['link_phone'] = $val['link_phone'];

            if (!empty($val['fid'])) {
                $data_exist = FamilySituations::where('id', $val['fid'])->first();
                if ($data_exist) {
                    // 更新信息
                    $result = FamilySituations::where('id', $val['fid'])->update($map);
                } else {
                    $map['resume_id'] = $id;
                    // 添加信息
                    $result = FamilySituations::create($map);
                }
            } else {
                $map['resume_id'] = $id;
                // 添加信息
                $result = FamilySituations::create($map);
            }
        }
    }

    /**
     * 删除
     * @param $request
     * @return bool\Intervention
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function destory($request)
    {
        $id = $request->input('id', '');
        $res = FamilySituations::destory($id);
        if ($res > 0) {
            return $res;
        } else {
            return false;
        }
    }

    /**
     * 恢复删除
     * @param int $id
     * @return mixed
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function restore($request)
    {
        $id = $request->input('id', '');
        $res = FamilySituations::where('id', $id)->restore();
        return $res;
    }

}
